<template>
  <div>
    <el-dialog title="新增福利包" center :visible.sync="dialogVisible" width="40%" :before-close="handleClose" @open="getDateils()">
      <el-form ref="form" :model="form" v-loading="formLoading" :rules="rules" label-width="120px">
        <el-form-item label="福利包名称" prop="free_name">
          <el-input v-model="form.free_name" maxlength="20" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="免费领规则" prop="free_type">
          <div class="flex">
            <el-select v-model="form.free_type" placeholder="请选择" style="marginRight:10px">
              <el-option v-for="(v,i) in ruleArr" :key="i" :label="v.label" :value="v.value">
              </el-option>
            </el-select>
            <el-input style="width:200px" v-model.number="form.free_num" placeholder="请输入"></el-input>
            <div style="marginLeft:10px">次</div>
          </div>
        </el-form-item>
        <el-form-item label="积分发放规则" prop="integral_type">
          <div class="flex">
            <el-select v-model="form.integral_type" placeholder="请选择" style="marginRight:10px">
              <el-option v-for="(v,i) in ruleArr" :key="i" :label="v.label" :value="v.value">
              </el-option>
            </el-select>
            <el-input style="width:200px" v-model="form.integral_num" oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')" placeholder="请输入"></el-input>
            <div style="marginLeft:10px">积分</div>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import authenticationService from '@/api/authenticationService.js'
export default {
  data () {
    return {
      form: {},
      ruleArr: [
        { label: "每月", value: 1 },
        { label: "一次性", value: 2 }
      ],
      rules: {
        free_name: [
          { required: true, message: '请输入福利包名称', trigger: 'blur' },
        ],
        free_type: [
          { required: true, message: '选择免费领规则', trigger: 'blur' },
        ],
        integral_type: [
          { required: true, message: '选择积分发放规则', trigger: 'blur' },
        ],
      },
      btnLoading: false,
      formLoading: false,
    }
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: {
      type: Number | String
    }
  },
  computed: {
    dialogVisible: {
      get () {
        return this.show
      },
      set (val) {
        this.$emit("update:show", val)
      }
    }
  },
  methods: {
    handleClose () {
      this.$refs.form.resetFields();
      this.form = {};
      this.dialogVisible = false
    },
    // 详情
    getDateils () {
      if (this.id) {
        this.formLoading = true;
        authenticationService.freebagInfo(this.id).then(res => {
          this.formLoading = false;
          if (res && res.data.code == 200) {
            this.form = res.data.data
          }
        })
      }
    },
    // 提交
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.id) {
            this.add()
          } else {
            this.update()
          }
        }
      });
    },
    // 新增
    add () {
      this.btnLoading = true;
      authenticationService.freebagAdd(this.form).then(res => {
        this.btnLoading = false;
        if (res && res.data.code == 200) {
          this.handleClose();
          this.$message.success('新增成功');
          this.$emit('change')
        }
      })
    },
    update () {
      this.btnLoading = true;
      authenticationService.freebagUpdate(this.form).then(res => {
        this.btnLoading = false;
        if (res && res.data.code == 200) {
          this.handleClose();
          this.$emit('change')
          this.$message.success('修改成功');
        }
      })
    }

  }
}
</script>

<style>
.el-form--inline .el-form-item__content {
  width: auto !important;
}
input[type='number'] {
  -moz-appearance: textfield;
}
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button {
  -webkit-appearance: none;
}
</style>